﻿using System.Collections.Generic;
using System.Linq;
using Biz.EF.Platform.SG;
using Biz.Process;
using Hjt212Parser;
using log4net;

namespace Biz.Buffer
{
    public static class CommonInformation
    {
        private static readonly ILog Log = LogManager.GetLogger(typeof(CommonInformation));

        private static List<企业排口> _letports;
        public static 企业排口 GetLetportByMn(string mn)
        {
            if (_letports == null || MyTimer.IsToTheTime("GetLetportByMn", 10))
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _letports = pCtx.企业排口.AsNoTracking().ToList();
                }
            }

            var byMn = _letports?.FirstOrDefault(t => t.MN号.Contains(mn));
            if (byMn == null)
            {
                Log.Info(mn + "->没有对应排口");
            }
            return byMn;
        }

        public static bool? IsNationalControl(string mn)
        {
            var port = GetLetportByMn(mn);
            if (port == null) return null;

            return port.排放口等级ID.HasValue && port.排放口等级ID == 1501;
        }
        private static List<企业基本信息> _pollutionSources;
        public static string GetPollutionSourceCodeByMn(string mn)
        {
            var port = GetLetportByMn(mn);
            if (port == null) return null;
            if (_pollutionSources == null || MyTimer.IsToTheTime("GetPollutionSourceCodeByMN", 10))
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _pollutionSources = pCtx.企业基本信息.AsNoTracking().ToList();
                }
            }
            var enterprise = _pollutionSources?.FirstOrDefault(t => t.ID == port.企业基本信息ID);
            if (enterprise == null)
            {
                Log.Info(mn + "->没有对应企业");
                return null;
            }
            return enterprise.污染源编码;

        }
        public static string GetPollutionSourceNameByMn(string mn)
        {
            var port = GetLetportByMn(mn);
            if (port == null) return null;
            if (_pollutionSources == null || MyTimer.IsToTheTime("GetPollutionSourceNameByMN", 10))
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _pollutionSources = pCtx.企业基本信息.AsNoTracking().ToList();
                }
            }
            var enterprise = _pollutionSources?.FirstOrDefault(t => t.ID == port.企业基本信息ID);
            if (enterprise == null)
            {
                Log.Info(mn + "->没有对应企业");
                return null;
            }
            return enterprise.企业名称;

        }

        private static List<污染物类别> _contamTypes;
        public static 污染物类别 GetContamType(string code)
        {
            if (_contamTypes == null || MyTimer.IsToTheTime("GetContamType", 10))
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _contamTypes = pCtx.污染物类别.AsNoTracking().ToList();
                }
            }
            var name = "污水";
            if (code.Contains("B01")) name = "污水";
            else if (code.Contains("B02")) name = "废气";

            return _contamTypes?.FirstOrDefault(t => t.类别名称.Contains(name));
        }
        private static List<污染物编码> _contamCodes;
        private static List<污染物对照> _mappings;
        public static 污染物编码 GetContamCode(string code)
        {
            if (_contamCodes == null || MyTimer.IsToTheTime("GetContamCode", 10))
            {
                using (var pCtx = new PlatformXpfContext())
                {
                    _contamCodes = pCtx.污染物编码.AsNoTracking().ToList();
                    _mappings = pCtx.污染物对照.AsNoTracking().ToList();
                }
            }

            var item = _contamCodes?.FirstOrDefault(t => t.污染物代码.Equals(code));
            if (item != null)
            {
                return item;
            }
            var map = _mappings?.FirstOrDefault(t => t.污染物新代码.Equals(code));
            if (map != null)
            {
                var contamCode = _contamCodes?.FirstOrDefault(t => t.污染物代码.Equals(map.污染物代码));
                if (contamCode != null)return contamCode;
            }
            Log.Info(code + "->没有对应污染物因子");
            return null;
        }
        private static List<报警异常数据类型> _alarmTypes;
        public static 报警异常数据类型 GetAlarmTypeByCode(string alarmName)
        {
            if (_alarmTypes == null || MyTimer.IsToTheTime("GetAlarmTypeByCode", 10))
            {
                using (var ctx=new PlatformXpfContext())
                {
                    _alarmTypes = ctx.报警异常数据类型.AsNoTracking().ToList();
                }
            }
            return _alarmTypes.FirstOrDefault(t => t.代码.Equals(alarmName));

        }
    }
}
